local niumaget = fk.CreateSkill {

  name = "joy_mou__niumaget",

  tags = {  },

}



niumaget:addEffect("viewas", {
  name = "joy_mou__niumaget",
  pattern = ".",
  expand_pile = function(self,player) 
    return player:getTableMark("joy_niuma")
  end,
  card_filter = function(self, player, to_select, selected)
    if #selected == 0 then
      for _, cid in ipairs(player:getTableMark("joy_niuma")) do
        if cid == to_select then
          local card = Fk:getCardById(to_select)
          if Fk.currentResponsePattern == nil then
            return player:canUse(card) and not player:prohibitUse(card)
          else
            return Exppattern:Parse(Fk.currentResponsePattern):match(card)
          end
        end
      end
    end
  end,
  view_as = function(self, player,cards)
    if #cards ~= 1 then return end
    return Fk:getCardById(cards[1])
  end,
  enabled_at_play = function(self, player)
    local mark = player:getTableMark("joy_niuma")
    for _, cid in ipairs(mark) do
      local to_use = Fk:cloneCard(Fk:getCardById(cid).name)
      to_use.skillName = niumaget.name
      if player:canUse(to_use) and not player:prohibitUse(to_use) then
        return true
      end
    end
    return false
  end,
  enabled_at_response = function(self, player, response)
    local mark = player:getTableMark("joy_niuma")
    if not response then
      for _, cid in ipairs(mark) do
        local to_use = Fk:cloneCard(Fk:getCardById(cid).name)
        to_use.skillName = niumaget.name
        if Fk.currentResponsePattern and Exppattern:Parse(Fk.currentResponsePattern):match(to_use) and
            not player:prohibitUse(to_use)  then
          return true
        end
      end
    else
      for _, cid in ipairs(mark) do
        local to_use = Fk:cloneCard(Fk:getCardById(cid).name)
        to_use.skillName = niumaget.name
        if Fk.currentResponsePattern and Exppattern:Parse(Fk.currentResponsePattern):match(to_use)  then
          return true
        end
      end
    end
    return false
  end,
})

niumaget:addEffect(fk.EventPhaseEnd, {
  name = "#joy_mou__niumaget_trigger",
  events = {fk.EventPhaseEnd},
  can_trigger = function (self, event, target, player, data)
    if target ~= player or not player:hasSkill(niumaget.name) then return end
    if event == fk.EventPhaseEnd and player.phase == Player.Play  then
      local cards = player.room.tag["joy_niuma"] or {}
      return not player:isKongcheng() and #cards < 5 and player.hp > 0
    end
  end,
  on_cost = function (self, event, target, player, data)
    local room = player.room
    if event == fk.EventPhaseEnd then
      local cards = player.room.tag["joy_niuma"] or {}
      local n = math.min(player.hp,5 - #cards)
      local card = room:askForCard(player,1,n,false,niumaget.name,true,".","你可将至多"..(n).."张手牌扣置于“流马”区")
      if #card > 0 then
        event:setCostData(self, card)
        return true
      end
    end
  end,
  on_use = function (self, event, target, player, data)
    local room = player.room
    if event == fk.EventPhaseEnd then
      AddToNMPile(room,event:getCostData(self), niumaget.name, player.id)
    end
  end,
})

return niumaget